package com.caipiao.lottery.lty.message;

/**
 * @author GaoFeng
 */

import java.security.PrivateKey;
import java.security.PublicKey;

import org.slf4j.Logger;
import com.caipiao.util.CodeHelper;
import com.caipiao.util.StringTools;
import com.caipiao.util.Zip;

public class MessageCodeHelper
{	
	public static String DecodeBody(String msgID, String encryptMode,
			String miData, String mingKey, Logger logger) throws Exception
	{
		if (encryptMode.equalsIgnoreCase("1"))
		{
			return DecodeMode1(msgID, miData, logger);
		} 
		else if (encryptMode.equalsIgnoreCase("2"))
		{
			return DecodeMode2(msgID, miData, logger);
		}
		else if (encryptMode.equalsIgnoreCase("3"))
		{
			return DecodeMode3(msgID, miData, mingKey, logger);
		}
		else if (encryptMode.equalsIgnoreCase("4"))
		{
			return DecodeMode4(msgID, miData, mingKey, logger);
		}
		return "";
	}

	public static String EncodeBody(String msgID, String encryptMode,
			String mingData, String mingKey, Logger logger) throws Exception
	{
		if (encryptMode.equalsIgnoreCase("1"))
		{
			return EncodeMode1(msgID, mingData, logger);
		} 
		else if (encryptMode.equalsIgnoreCase("2"))
		{
			return EncodeMode2(msgID, mingData, logger);
		}
		else if (encryptMode.equalsIgnoreCase("3"))
		{
			return EncodeMode3(msgID, mingData, mingKey, logger);
		}
		else if (encryptMode.equalsIgnoreCase("4"))
		{
			return EncodeMode4(msgID, mingData, mingKey, logger);
		}

		return "";
	}
	
	public static String DecodeMode1(String msgID, String oriData, Logger logger)throws Exception
	{
		String strMing = "";
		byte[] byteMing = null;
		try
		{
			logger.trace("DecodeMode1:" + msgID + " ORI:" + oriData);
			
			byteMing = CodeHelper.Base64Decode(oriData);
			
			strMing = new String(byteMing, "UTF8");

			logger.trace("DecodeMode1:" + msgID + " Base64De:" + strMing);

		}
		finally
		{
			byteMing = null;
		}
		return strMing;
	}

	public static String DecodeMode2(String msgID, String oriData, Logger logger) throws Exception
	{
		String strMing = "";
		byte[] byteMing = null;
		byte[] byteMi = null;
		try
		{
			logger.trace("DecodeMode2:" + msgID + " ORI:" + oriData);
			
			byteMi = CodeHelper.Base64Decode(oriData);

			logger.trace("DecodeMode2:" + msgID + " Base64De:" + StringTools.bytesToHexString(byteMi));
			
			byteMing = Zip.unjzlib(byteMi);
			
			strMing = new String(byteMing, "UTF8");
			
			logger.trace("DecodeMode2:" + msgID + " UnZip:" + strMing);
			
		} finally
		{
			byteMing = null;
			byteMi = null;
		}
		return strMing;
	}

	public static String DecodeMode3(String msgID, String oriData, String strKey, Logger logger) throws Exception
	{
		String strMing = "";
		byte[] byteMing = null;
		byte[] byteMi = null;
		byte[] byteKey = null;
		
		try
		{
			logger.trace("DecodeMode3:" + msgID + " ORI:" + oriData);
			
			byteMi = CodeHelper.Base64Decode(oriData);
			
			logger.trace("DecodeMode3:" + msgID + " Base64De:" + StringTools.bytesToHexString(byteMi));
			
			byteKey = StringTools.hexStringToByte(strKey);
			
			byteMing = CodeHelper.AesDecrypt(byteMi, byteKey);
			
			strMing = new String(byteMing, "UTF8");
			
			logger.trace("DecodeMode3:" + msgID + " AesDecrypt:" + strMing);
		}
		finally
		{
			byteMing = null;
			byteMi = null;
			byteKey = null;
		}
		return strMing;
	}
	
	public static String DecodeMode4(String msgID, String oriData, String strKey, Logger logger) throws Exception
	{
		String strMing = "";
		byte[] byteMing = null;
		byte[] byteMi = null;
		byte[] byteKey = null;
		
		try
		{
			logger.trace("DecodeMode4:" + msgID + " ORI:" + oriData);
			
			byteMi = CodeHelper.Base64Decode(oriData);
			
			logger.trace("DecodeMode4:" + msgID + " Base64De:" + StringTools.bytesToHexString(byteMi));
			
			byteKey = StringTools.hexStringToByte(strKey);
			
			byteMing = CodeHelper.AesDecrypt(byteMi, byteKey);
			
			logger.trace("DecodeMode4:" + msgID + " AesDecrypt:" + StringTools.bytesToHexString(byteMing));
			
			byteMing = Zip.unjzlib(byteMing);
			
			strMing = new String(byteMing, "UTF8");
			
			logger.trace("DecodeMode4:" + msgID + " UnZip:" + strMing);
		}
		finally
		{
			byteMing = null;
			byteMi = null;
			byteKey = null;
		}
		return strMing;
	}
	
	public static String EncodeMode1(String msgID, String oriData, Logger logger) throws Exception
	{
		String strMi = "";
		byte[] byteMing = null;
		
		try
		{
			logger.trace("EncodeMode1:" + msgID + " ORI:" + oriData);
			
			byteMing = oriData.getBytes("UTF8");
			
			strMi = CodeHelper.Base64Encode(byteMing);
			
			logger.trace("EncodeMode1:" + msgID + " Base64En:" + strMi);
		}
		finally
		{
			byteMing = null;
		}
		return strMi;
	}

	public static String EncodeMode2(String msgID, String oriData, Logger logger) throws Exception
	{
		String strMi = "";
		byte[] byteMi = null;
		byte[] byteMing = null;
		
		try
		{
			logger.trace("EncodeMode2:" + msgID + " ORI:" + oriData);
			
			byteMing = oriData.getBytes("UTF8");
			
			byteMi = Zip.jzlib(byteMing);
			
			logger.trace("EncodeMode2:" + msgID + " Zip:" + StringTools.bytesToHexString(byteMi));
			
			strMi = CodeHelper.Base64Encode(byteMi);
			
			logger.trace("EncodeMode2:" + msgID + " Base64En:" + strMi);
		}
		finally
		{
			byteMing = null;
			byteMi = null;
		}
		return strMi;
	}

	public static String EncodeMode3(String msgID, String oriData, String strKey, Logger logger) throws Exception
	{
		String strMi = "";
		byte[] byteMing = null;
		byte[] byteMi = null;
		byte[] byteKey = null;
		
		try
		{
			logger.trace("EncodeMode3:" + msgID + " ORI:" + oriData);
			
			byteMing = oriData.getBytes("UTF8");
			
			byteKey = StringTools.hexStringToByte(strKey);
			
			byteMi = CodeHelper.AesEncrypt(byteMing, byteKey);
			
			logger.trace("EncodeMode3:" + msgID + " AesEncrypt:" 	+ StringTools.bytesToHexString(byteMi));
			
			strMi = CodeHelper.Base64Encode(byteMi);
			
			logger.trace("EncodeMode3:" + msgID + " Base64En:" + strMi);
		}
		finally
		{
			byteMing = null;
			byteMi = null;
			byteKey = null;
		}
		return strMi;
	}

	public static String EncodeMode4(String msgID, String oriData, String strKey, Logger logger) throws Exception
	{
		String strMi = "";
		byte[] byteMing = null;
		byte[] byteMi = null;
		byte[] byteKey = null;
		
		try
		{
			logger.trace("EncodeMode4:" + msgID + " ORI:" + oriData);
			
			byteMing = oriData.getBytes("UTF8");
			
			byteMing = Zip.jzlib(byteMing);
			
			logger.trace("EncodeMode4:" + msgID + " Zip:" + StringTools.bytesToHexString(byteMing));
			
			byteKey = StringTools.hexStringToByte(strKey);
			
			byteMi = CodeHelper.AesEncrypt(byteMing, byteKey);
			
			logger.trace("EncodeMode4:" + msgID + " AesEncrypt:" 	+ StringTools.bytesToHexString(byteMi));
			
			strMi = CodeHelper.Base64Encode(byteMi);
			
			logger.trace("EncodeMode4:" + msgID + " Base64En:" + strMi);
		}
		finally
		{
			byteMing = null;
			byteMi = null;
			byteKey = null;
		}
		return strMi;
	}

	public static String DecodeKey(String msgID, String oriData, PrivateKey pKey, Logger logger) throws Exception
	{
		String strMing = "";
		byte[] byteMing = null;
		byte[] byteMi = null;
		try
		{
			logger.trace("DecodeKey:" + msgID + " ORI:" + oriData);
			
			byteMi = CodeHelper.Base64Decode(oriData);
			
			logger.trace("DecodeKey:" + msgID + " Base64De:" + StringTools.bytesToHexString(byteMi));
			
			byteMing = CodeHelper.RsaDecrypt(byteMi, pKey);
			
			strMing = new String(byteMing, "UTF8");
			
			logger.trace("DecodeKey:" + msgID + " RsaDecrypt:" + strMing);
		}
		finally
		{
			byteMing = null;
			byteMi = null;
		}
		return strMing;
	}

	public static String EncodeKey(String msgID, String oriData, PublicKey pKey, Logger logger) throws Exception
	{
		String strMi = "";
		byte[] byteMing = null;
		byte[] byteMi = null;
		try
		{
			logger.trace("EncodeKey:" + msgID + " ORI:" + oriData);
			
			byteMing = oriData.getBytes("UTF8");
			
			byteMi = CodeHelper.RsaEncrypt(byteMing, pKey);
			
			logger.trace("EncodeKey:" + msgID + " RsaEncrypt:" + StringTools.bytesToHexString(byteMi));

			strMi = CodeHelper.Base64Encode(byteMi);
			
			logger.trace("EncodeKey:" + msgID + " Base64En:" + strMi);
		}
		finally
		{
			byteMing = null;
			byteMi = null;
		}
		return strMi;
	}
}
